package com.example.administrator.fastdream.date.presenter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.example.administrator.fastdream.date.bean.BaseBean;
import com.example.administrator.fastdream.date.bean.PersonalInfoBean;
import com.example.administrator.fastdream.date.bean.UploadPhotoBean;
import com.example.administrator.fastdream.date.http.JsonCallback;
import com.example.administrator.fastdream.date.utils.CacheUtils;
import com.example.administrator.fastdream.date.utils.MyUrl;
import com.example.administrator.fastdream.view.activity.LoginActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.io.File;

public class PersonalInfoPresenter {
    private Activity mActivity;
    private PersonCallBack callBack;

    public PersonalInfoPresenter(Activity activity, PersonCallBack callBack) {
        this.mActivity = activity;
        this.callBack = callBack;
    }

    public void uploadPhotos(File file) {
        HttpParams params = new HttpParams();
        params.put("files[]", file);
        OkGo.<UploadPhotoBean>post(MyUrl.BASEURL + "Home/upload_img")
                .tag(this)
                .params(params)
                .execute(new JsonCallback<UploadPhotoBean>(UploadPhotoBean.class) {
                    @Override
                    public void onSuccess(Response<UploadPhotoBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            callBack.uploadPhotos(response.body());
                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public void getPersonalInfo() {
        HttpParams params = new HttpParams();
        params.put("user_token", (String) CacheUtils.get("token"));
        OkGo.<PersonalInfoBean>post(MyUrl.BASEURL + "Home/get_user_info")
                .tag(this)
                .params(params)
                .execute(new JsonCallback<PersonalInfoBean>(PersonalInfoBean.class) {
                    @Override
                    public void onSuccess(Response<PersonalInfoBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            callBack.getPersonalInfo(response.body());
                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public void savePersonalInfo(String image, String username, String city_name, String address) {
        HttpParams params = new HttpParams();
        params.put("user_token", (String) CacheUtils.get("token"));
        if (!TextUtils.isEmpty(image))
            params.put("avatar", image);
        if (!TextUtils.isEmpty(username))
            params.put("username", username);
        if (!TextUtils.isEmpty(city_name))
            params.put("city_name", city_name);
        if (!TextUtils.isEmpty(address))
            params.put("address", address);
        OkGo.<BaseBean>post(MyUrl.BASEURL + "Home/upd_user_info")
                .tag(this)
                .params(params)
                .execute(new JsonCallback<BaseBean>(BaseBean.class) {
                    @Override
                    public void onSuccess(Response<BaseBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            callBack.savePersonalInfo(response.body());
                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public interface PersonCallBack {
        void getPersonalInfo(PersonalInfoBean personalInfoBean);

        void savePersonalInfo(BaseBean baseBean);

        void uploadPhotos(UploadPhotoBean uploadPhotoBean);
    }
}
